﻿
using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Windows_System_Snapshot
{
	/// <summary>
	/// Description of Settings.
	/// </summary>
	public partial class Settings : Form
	{

		
		public Settings()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			// 
			try
			{
				timeoutTB.Text = LogicAbstract.timeOutSec.ToString();
				
				if (LogicAbstract.cards != null)
				{
	                // fill in the combobox
	                for (int i = 0 ; i < LogicAbstract.cards.Count ; i++)
	                {
	                	clientCardsComboBox.Items.Add(((Card)LogicAbstract.cards[i]).name);
	                }
	                clientCardsComboBox.SelectedIndex = 0;
	            }
				
				if (LogicAbstract.perfConfig != null)
				{
					if (LogicAbstract.perfConfig.runPerfTests)
					{
						checkBox1.Checked = true;
					}
					serverIP.Text = LogicAbstract.perfConfig.serverIP;
					if (LogicAbstract.perfConfig.clinetIbPortNum.Equals("1"))
					{
						port1.Checked = true;
					}
					else
					{
						port2.Checked = true;
					}
					for (int i = 0 ; i < LogicAbstract.cards.Count ; i++)
					{
						if (((Card)LogicAbstract.cards[i]).nodeGuid.Equals( LogicAbstract.perfConfig.clinetIbDevGuid))
						{
							clientCardsComboBox.SelectedIndex = i;
							break;
						}
					}
				}
			}
			catch{}
		}
		
		void CancelBtnClick(object sender, EventArgs e)
		{
			// close the settings window
			this.Dispose();
		}
		
		void OkBtnClick(object sender, EventArgs e)
		{
			try
			{
				// timeout
				try
				{
					LogicAbstract.timeOutSec = Int32.Parse(timeoutTB.Text);
				}
				catch
				{
					MessageBox.Show("Timeout:\nPlease enter only an integer alue.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
					return;
				}
				if (checkBox1.Checked)
				{
					// perf config 
					if (serverIP.Text.Equals(""))
					{
						MessageBox.Show("Please enter the IP address of host that is running 'ibv_write_bw' and 'ibv_write_lat' in server mode.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
						return;
					}
					LogicAbstract.perfConfig = new PerformanceConf();
					LogicAbstract.perfConfig.serverIP = serverIP.Text;
					if (LogicAbstract.cards != null && LogicAbstract.cards.Count > 0)
					{
						LogicAbstract.perfConfig.clinetIbDevGuid = ((Card)LogicAbstract.cards[clientCardsComboBox.SelectedIndex]).nodeGuid;
						LogicAbstract.perfConfig.clinetIbDevName = ((Card)LogicAbstract.cards[clientCardsComboBox.SelectedIndex]).name;
						if (port1.Checked)
						{
							LogicAbstract.perfConfig.clinetIbPortNum = "1";
						}
						else
						{
							LogicAbstract.perfConfig.clinetIbPortNum = "2";
						}
						LogicAbstract.perfConfig.runPerfTests = true;
					}
					else
					{
						//MessageBox.Show("There is no available IB device.\nThe performace tests will not be performed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
						LogicAbstract.perfConfig = null;
					}
				}
				this.Dispose();
			}
			catch{}
		}
		
		void ClientCardsComboBoxSelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				int index = clientCardsComboBox.SelectedIndex;
				
				// select port #1 by default
				port1.Checked = true;
				
				// check if the choosen card has only one port, and disable the second radio button
				if (((Card)LogicAbstract.cards[index]).physPortCount == 1)
				{
					port2.Enabled = false;
				}
				else
				{
					port2.Enabled = true;
				}
			}
			catch{}
		}
				
		void CheckBox1CheckedChanged(object sender, EventArgs e)
		{
			try
			{
				if (checkBox1.Checked && LogicAbstract.cards == null)
				{
					MessageBox.Show("Failed to get list of IB devices.\nThe performace tests will not be performed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
					checkBox1.Checked = false;
				}
				else
				{
					if (checkBox1.Checked)
					{
						clinetGroupBox.Enabled = true;
						serverGroupBox.Enabled = true;
						if (LogicAbstract.perfConfig != null)
						{
							LogicAbstract.perfConfig.runPerfTests = true;
						}
					}
					else
					{
						clinetGroupBox.Enabled = false;
						serverGroupBox.Enabled = false;
						if (LogicAbstract.perfConfig != null)
						{
							LogicAbstract.perfConfig.runPerfTests = false;
						}
					}
				}
			}
			catch{}
		}
		
	}
}
